package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.datamediapair.DataMediaPairService;
import com.alibaba.otter.manager.web.dto.*;
import com.alibaba.otter.manager.web.service.DataMediaPairAggService;
import com.alibaba.otter.shared.common.model.config.data.DataMediaPair;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RequestMapping("/api/v1/data-media-pair")
@RestController
public class DataMediaPairResource {


    private final DataMediaPairService dataMediaPairService;

    private final DataMediaPairAggService dataMediaPairAggService;



    public DataMediaPairResource(DataMediaPairService dataMediaPairService, DataMediaPairAggService dataMediaPairAggService) {
        this.dataMediaPairService = dataMediaPairService;
        this.dataMediaPairAggService = dataMediaPairAggService;
    }


    /**
     * 根据id查找树
     */
    @GetMapping("findOne/{id}")
    public RestResponse<DataMediaPair> findOne(@PathVariable("id") Long id) {
        return RestResponse.ok(dataMediaPairService.findById(id));
    }


    @GetMapping("find-column-pair")
    public RestResponse<ColumnPairDTO> findColumnPair(@RequestParam("dataMediaPairId") Long dataMediaPairId,
                                                      @RequestParam("sourceMediaId") Long sourceMediaId,
                                                      @RequestParam("targetMediaId") Long targetMediaId) {
        return RestResponse.ok(dataMediaPairAggService.queryColumnPair(dataMediaPairId,sourceMediaId,targetMediaId));
    }


    /**
     * 根据条件查询列表数据
     */
    @GetMapping("list")
    public RestResponse<List<DataMediaPairDTO>> list(@RequestParam("pipelineId") Long pipelineId) throws Exception {
        return RestResponse.ok(dataMediaPairAggService.list(pipelineId));
    }


    /**
     * 根据条件查询列表数据
     */
    @GetMapping("query-column-group")
    public RestResponse<ColumnGroupDTO> queryColumnGroup(@RequestParam("dataMediaPairId") Long dataMediaPairId,
                                                         @RequestParam("sourceMediaId") Long sourceMediaId,
                                                         @RequestParam("targetMediaId") Long targetMediaId) {
        return RestResponse.ok(dataMediaPairAggService.queryColumnGroup(dataMediaPairId, sourceMediaId, targetMediaId));
    }


    /**
     * 添加
     */
    @PostMapping("batch-create/{pipelineId}")
    public RestResponse<Void> batchCreate(@RequestBody List<String> batchPairContent, @PathVariable("pipelineId") Long pipelineId) {
        dataMediaPairAggService.batchCreate(batchPairContent, pipelineId);
        return RestResponse.ok();
    }


    /**
     * 添加
     */
    @PostMapping("create")
    public RestResponse<Long> create(@RequestBody @Validated DataMediaPair dbMediaSource) {
        return RestResponse.ok(dataMediaPairService.createAndReturnId(dbMediaSource));
    }


    /**
     * 添加
     */
    @PostMapping("column-pair/create")
    public RestResponse<Void> createColumnPair(@RequestBody @Validated ColumnPairDTO groupDTO) {
        dataMediaPairAggService.createColumnPair(groupDTO);
        return RestResponse.ok();
    }


    /**
     * 添加
     */
    @PostMapping("column-group/create")
    public RestResponse<Void> createColumnGroup(@RequestBody @Validated CreateColumnGroupDTO groupDTO) {
        dataMediaPairAggService.createColumnGroup(groupDTO);
        return RestResponse.ok();
    }


    /**
     * 编辑
     */
    @PutMapping("modify")
    public RestResponse<Void> update(@RequestBody @Validated DataMediaPair dbMediaSource) {
        dataMediaPairService.modify(dbMediaSource);
        return RestResponse.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id) {
        dataMediaPairService.remove(id);
        return RestResponse.ok();
    }


}
